package com.tiger.admin.http.apps;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tiger.admin.Db;
import com.tiger.common.constant.SourceOfData;
import com.tiger.db.entity.App;
import com.tiger.db.entity.AppCategory;
import com.tiger.db.entity.SchoolDept;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AppService {
    private final Db db;

    public AppService(Db db) {
        this.db = db;
    }

    public void importApps(MultipartFile file, Integer category, boolean isTaskApp) {

        List<App> sheetData = new ArrayList<>();

        try (Workbook workbook = new XSSFWorkbook(file.getInputStream())) {
            for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
                Sheet sheet = workbook.getSheetAt(i);
                readSheet(sheet, sheetData, category, isTaskApp);
            }

            List<String> appIds = new ArrayList<>();

            for(App row : sheetData) {
                if(!appIds.contains(row.getSourceKey())) {
                    appIds.add(row.getSourceKey());
                }
            }



            if(!appIds.isEmpty()) {
                List<App> apps = db.app.list(new LambdaQueryWrapper<App>().select(App::getId, App::getSourceKey).in(App::getSourceKey, appIds));

                if(!apps.isEmpty()) {
                    appIds = apps.stream().map(App::getSourceKey).collect(Collectors.toList());
                    List<App> datas = new ArrayList<>();
                    for(App row : sheetData) {
                        if(!appIds.contains(row.getSourceKey())) {
                            datas.add(row);
                        }
                    }

                    if(!datas.isEmpty()) {
                        db.app.saveBatch(datas);
                        return;
                    }

                    return;
                }
            }


            db.app.saveBatch(sheetData);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    private void readSheet(Sheet sheet, List<App> sheetData, Integer category, boolean isTaskApp) {
        for (Row row : sheet) {
            if(row.getRowNum() == 0) continue;

            App app = new App();
            app.setEnable(true);
            app.setSource(SourceOfData.IMPORT);
            app.setCategory(category);
            app.setTaskApp(isTaskApp);

            Cell cell0 = row.getCell(0);

            if(cell0 != null) {
                app.setSourceKey(cell0.getStringCellValue()); //appId
            }

            if(app.getSourceKey() == null || app.getSourceKey().isEmpty()) {
                continue;
            }

            Cell cell1 = row.getCell(1); //appName
            if(cell1 != null) {
                app.setName(cell1.getStringCellValue());
            }

            if(app.getName() == null || app.getName().isEmpty()) {
                continue;
            }


            Cell cell2 = row.getCell(2);
            if(cell2 != null) {
                String v = cell2.getStringCellValue();
                if(v != null && !v.isEmpty()) {
                    app.setUrl(v);
                }
            }


            sheetData.add(app);

        }
    }


    public void importApps1(MultipartFile file) {
        List<AppCategory> categories = db.appCategory.list();
        HashMap<String, Integer> cateMap = new HashMap<>();
        for(AppCategory category : categories) {

            String name = "common";

            if(category.getName().contains("教职工")) {
                name = "teacher";
            }else if(category.getName().contains("学生")) {
                name = "student";
            }

            cateMap.put(name, category.getId());
        }

        List<App> sheetData = new ArrayList<>();

        try (Workbook workbook = new XSSFWorkbook(file.getInputStream())) {
            for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
                Sheet sheet = workbook.getSheetAt(i);
                readSheet1(sheet, sheetData, cateMap);
            }


            db.app.saveBatch(sheetData);



        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    private void readSheet1(Sheet sheet, List<App> sheetData, HashMap<String, Integer> cateMap) {
        for (Row row : sheet) {
            if(row.getRowNum() == 0) continue;

            App app = new App();
            app.setEnable(true);
            app.setSource(SourceOfData.IMPORT);

            sheetData.add(app);

            Cell cell0 = row.getCell(0);

            if(cell0 != null) {
                app.setSourceKey(cell0.getStringCellValue());
            }

            Cell cell1 = row.getCell(1);
            if(cell1 != null) {
                app.setName(cell1.getStringCellValue());
            }

            app.setCategory(cateMap.get("common"));


            Cell cell2 = row.getCell(2);
            if(cell2 != null) {
                String v = cell2.getStringCellValue();
                if(v != null && !v.isEmpty()) {
                    boolean containsTeacher = v.contains("老师");
                    boolean containsStudent = v.contains("学生");
                    if(!containsTeacher || !containsStudent) {
                        if(containsTeacher) {
                            app.setCategory(cateMap.get("teacher"));
                        }else if(containsStudent) {
                            app.setCategory(cateMap.get("student"));
                        }
                    }
                }
            }

            Cell cell3 = row.getCell(3);
            if(cell3 != null) {
                String v = cell3.getStringCellValue();
                if(v != null && !v.isEmpty()) {
                    app.setUrl(v);
                }
            }
        }
    }
}
